<template>
  <div>
    <!-- 表格 -->
    <el-table style="width: 100%" border :data="skuList">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="200">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column
      ><el-table-column prop="prop" label="默认图片" width="150">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px" />
        </template> </el-table-column
      ><el-table-column prop="weight" label="重量（kg)" width="100">
      </el-table-column
      ><el-table-column prop="price" label="价格（元）" width="100">
      </el-table-column
      ><el-table-column prop="prop" label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-tooltip
            content="上架"
            placement="bottom"
            effect="light"
            v-if="!row.isSale == 1"
          >
            <el-button
              type="warning"
              icon="el-icon-sort-up"
              size="mini"
              @click="onSale(row)"
              circle
            ></el-button>
          </el-tooltip>
          <el-tooltip content="下架" placement="bottom" effect="light" v-else>
            <el-button
              type="warning"
              icon="el-icon-sort-down"
              size="mini"
              @click="cancelSale(row)"
              circle
            ></el-button>
          </el-tooltip>
          <el-tooltip content="查看详情" placement="bottom" effect="light">
            <el-button
              type="info"
              icon="el-icon-search"
              size="mini"
              @click="getSkuInfo(row)"
              circle
            ></el-button>
          </el-tooltip>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            circle
          ></el-button>
          <el-tooltip content="删除" placement="bottom" effect="light">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      style="text-align: center"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    >
    </el-pagination>

    <!-- sku详情 -->
    <el-drawer :visible.sync="drawer" :with-header="false" size="50%">
      <el-row :gutter="20">
        <el-col :span="5"><strong>名称</strong></el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5"><strong>详情</strong></el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5"><strong>平台描述</strong></el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            v-for="attr in skuInfo.skuAttrValueList"
            :key="attr.id"
            style="margin-right: 5px"
            >{{ attr.attrName }}--{{ attr.valueName }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5"><strong>价格</strong></el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5"><strong>商品图片</strong></el-col>
        <el-col :span="16">
          <el-carousel height="400px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img
                :src="item.imgUrl"
                alt=""
                style="width: 100%; height: 100%"
              />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1, //代表当前第几页
      limit: 5, //代表当前页展示多少条数据
      skuList: [], //存储sku列表数据
      total: 0, //总共多少条数据
      // 控制sku详情显示与隐藏
      drawer: false,
      // 存储sku详情
      skuInfo: {},
    };
  },
  methods: {
    // 发请求，获取sku数据
    async getSkuList() {
      let result = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (result.code == 200) {
        this.skuList = result.data.records;
        this.total = result.data.total;
      }
    },
    //当前页改变时的回调
    handleCurrentChange(page) {
      this.page = page;
      // 重新发请求
      this.getSkuList();
    },
    // 当每页显示发生变化时的回调
    handleSizeChange(limit) {
      this.limit = limit;
      // 重新发请求
      this.getSkuList();
    },
    // 上架
    async onSale(row) {
      let result = await this.$API.sku.reqOnSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message.success("上架成功");
      }
    },
    // 下架
    async cancelSale(row) {
      let result = await this.$API.sku.reqCancelSale(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message.success("下架成功");
      }
      row.isSale = 0;
    },
    // 获取sku详情
    async getSkuInfo(row) {
      // 显示详情框
      this.drawer = true;
      let result = await this.$API.sku.reqGetSkuById(row.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
  },
  mounted() {
    // 组件挂在完毕，发一次请求
    this.getSkuList();
  },
};
</script>

<style>
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.el-carousel__button {
  background: green;
  width: 5px;
  height: 5px;
  border-radius: 50%;
}
</style>

<style>
.el-col {
  margin: 10px 0;
}
.el-col-5 {
  font-size: 20px;
  text-align: right;
}
.el-col-16 {
  line-height: 30px;
}
</style>